﻿// Copyright 2012 T4C: sejerpz@gmail.com
// Licensed under GPL v3

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SET.Statistics;
using SET.Core;

namespace SET
{
    partial class FormStatistics : Form
    {
        private Database statistics;

        private class QuickSelectPeriod
        {
            public int Interval { get; set; }
            public string Description { get; set; }

            public QuickSelectPeriod(int interval, string description)
            {
                this.Interval = interval;
                this.Description = description;
            }
        }

        private FormStatistics()
        {
            InitializeComponent();
        }

        public FormStatistics(Database statistics)
            : this()
        {
            this.statistics = statistics;
            UpdateStatistics();
        }


        private void UpdateStatistics()
        {
            DataTable results = statistics.GetSummaryData(Excercise.Intervals, DateTime.Today, DateTime.Now);
            int correctCount =  Convert.ToInt32(GetValue(results, 0, "CorrectCount"));
            int totalCount = Convert.ToInt32(GetValue(results, 0, "TotalCount"));
            labelTodayCorrect.Text = correctCount.ToString();
            labelTodayTotal.Text = totalCount.ToString();
            if (totalCount == 0)
                labelTodayPerc.Text = "";
            else
                labelTodayPerc.Text = string.Format("{0} %", Math.Round((double)correctCount / (double)totalCount * 100).ToString());

            results = statistics.GetSummaryData(Excercise.Intervals, DateTime.Today.AddDays(-7), DateTime.Now);
            correctCount = Convert.ToInt32(GetValue(results, 0, "CorrectCount"));
            totalCount = Convert.ToInt32(GetValue(results, 0, "TotalCount"));
            labelLast7Correct.Text = correctCount.ToString();
            labelLast7Total.Text = totalCount.ToString();
            if (totalCount == 0)
                labelLast7Perc.Text =  "";
            else
                labelLast7Perc.Text =  string.Format("{0} %", Math.Round((double)correctCount / (double)totalCount * 100).ToString());

            results = statistics.GetSummaryData(Excercise.Intervals, DateTime.Today.AddDays(-30), DateTime.Now);
            correctCount = Convert.ToInt32(GetValue(results, 0, "CorrectCount"));
            totalCount = Convert.ToInt32(GetValue(results, 0, "TotalCount"));
            labelLast30Correct.Text = correctCount.ToString();
            labelLast30Total.Text = totalCount.ToString();
               if (totalCount == 0)
                labelLast30Perc.Text = "";
            else
                labelLast30Perc.Text = string.Format("{0} %", Math.Round((double)correctCount / (double)totalCount * 100).ToString());

            results = statistics.GetSummaryData(Excercise.Intervals, DateTime.MinValue, DateTime.MaxValue);
            correctCount = Convert.ToInt32(GetValue(results, 0, "CorrectCount"));
            totalCount = Convert.ToInt32(GetValue(results, 0, "TotalCount"));
            labelTotalCorrect.Text = correctCount.ToString();
            labelTotalTotal.Text = totalCount.ToString();
            if (totalCount == 0)
                labelTotalPerc.Text = "";
            else
                labelTotalPerc.Text = string.Format("{0} %", Math.Round((double)correctCount / (double)totalCount * 100).ToString());
        }

        private object GetValue(DataTable table, object defaultValue, string columnName, int rowIndex = 0)
        {
            if (table.Rows.Count <= rowIndex)
                return defaultValue;

            if (table.Rows[rowIndex][columnName] == DBNull.Value)
                return defaultValue;

            return table.Rows[rowIndex][columnName];
        }

        private void buttonTodayDetails_Click(object sender, EventArgs e)
        {
            ShowDetailsToday();
        }

        private void buttonLast7Details_Click(object sender, EventArgs e)
        {
            ShowDetailsLast7();
        }

        private void buttonLast30Details_Click(object sender, EventArgs e)
        {
            ShowDetailsLast30();
        }

        private void buttonTotalDetails_Click(object sender, EventArgs e)
        {
            ShowDetailsTotal();
        }

        private void ShowDetailsToday()
        {
            ShowDetails(Messages.ResourceManager.GetString("Today".ToValidIdentifier()), statistics.GetData(Excercise.Intervals, DateTime.Today, DateTime.Now));
        }


        private void ShowDetailsLast7()
        {
            ShowDetails(Messages.ResourceManager.GetString("Last 7 days".ToValidIdentifier()), statistics.GetData(Excercise.Intervals, DateTime.Today.AddDays(-7), DateTime.Now));
        }

        private void ShowDetailsLast30()
        {
            ShowDetails(Messages.ResourceManager.GetString("Last 30 days".ToValidIdentifier()), statistics.GetData(Excercise.Intervals, DateTime.Today.AddDays(-30), DateTime.Now));
            
        }

        private void ShowDetailsTotal()
        {
            ShowDetails(Messages.ResourceManager.GetString("All".ToValidIdentifier()), statistics.GetData(Excercise.Intervals, DateTime.MinValue, DateTime.MaxValue));
        }


        private void ShowDetails(string period, DataTable dataTable)
        {
            using (var form = new FormStatisticsDetails(period, dataTable))
            {
                form.ShowDialog(this);
            }
        }
    }
}
